package com.uairobot.bill.yz.office.agent.controller;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.uairobot.bill.yz.office.agent.entity.YzPreview;
import com.uairobot.bill.yz.office.agent.entity.YzTestUpload;
import com.uairobot.bill.yz.office.agent.service.YzOfficeService;
import com.uairobot.bill.yz.office.agent.service.YzPreviewService;
import com.uairobot.bill.yz.office.agent.utils.ContentTypeUtil;

import lombok.extern.slf4j.Slf4j;

/**
 * @author bill robot
 * @date 2020年1月3日_下午3:48:03 
 * @version 1.0 
 * @desc
 */
@RestController
@Slf4j
public class YzOfficeController {
  @Value("${yz.office.addr}")
  private String yzOfficeAddr;
  @Autowired
  private YzOfficeService yos;
  @Autowired
  private YzPreviewService yps;

  @RequestMapping("testUpload")
  public String testUpload(@RequestParam(value = "file") MultipartFile file) {
    YzTestUpload testUpload = yos.testUpload(file);
    return testUpload.getJson();
  }

  @RequestMapping("convert")
  public String convert(@RequestParam Map<String, String> params, HttpServletRequest request) {
    StringBuffer requestURL = request.getRequestURL();
    String projectUrl = requestURL.toString().replace("/convert", "");
    String json = yos.convert(params);
    json = json.replace(yzOfficeAddr, projectUrl);
    log.info("json : " + json);
    return json;
  }

  @RequestMapping("preview")
  public YzPreview preview(@RequestParam(value = "file") MultipartFile file) {
    // StringBuffer requestURL = request.getRequestURL();
    // String projectUrl = requestURL.toString().replace("/preview", "");
    YzPreview preview = yos.preview(file);
    return preview;
  }

  // view/{year}
  @RequestMapping("preview/{id}")
  public YzPreview getById(@PathVariable String id) {
    return yps.getById(id);
  }

  // view/2020/01/03/MjAwMTAzNDk2MDkzMDE5.html
  @RequestMapping("view/{year}/{month}/{day}/{name}.{extension}")
  public void view(@PathVariable String year, @PathVariable String month, @PathVariable String day, @PathVariable String name,
      @PathVariable String extension, HttpServletResponse response) {
    response.setContentType(ContentTypeUtil.getContentType(extension));
    String downloadAddr = yzOfficeAddr + "/view/" + year + "/" + month + "/" + day + "/" + name + "." + extension;
    try (OutputStream outputStream = response.getOutputStream()) {
      yos.download(downloadAddr, outputStream, null, name, extension);
      outputStream.flush();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  // view/2020/01/03/MjAwMTAzNTg1MzczNTU0.files/bootstrap.min.css
  @RequestMapping("view/{year}/{month}/{day}/{folderName}/{name}.{extension}")
  public void view(@PathVariable String year, @PathVariable String month, @PathVariable String day,
      @PathVariable String folderName, @PathVariable String name, @PathVariable String extension, HttpServletResponse response) {
    response.setContentType(ContentTypeUtil.getContentType(extension));
    String downloadAddr = append(yzOfficeAddr, "view", year, month, day, folderName, name) + "." + extension;
    log.info("downloadAddr : " + downloadAddr);
    try (OutputStream outputStream = response.getOutputStream()) {
      yos.download(downloadAddr, outputStream, folderName, name, extension);
      outputStream.flush();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  // view/2020/01/03/MjAwMTAzNDA3MDI5OTU0.files/res/images/pngImage33.png
  @RequestMapping("view/{year}/{month}/{day}/{folderName}/{res}/{images}/{name}.{extension}")
  public void view(@PathVariable String year, @PathVariable String month, @PathVariable String day,
      @PathVariable String folderName, @PathVariable String res, @PathVariable String images, @PathVariable String name,
      @PathVariable String extension, HttpServletResponse response) {
    response.setContentType(ContentTypeUtil.getContentType(extension));
    String downloadAddr = append(yzOfficeAddr, "view", year, month, day, folderName, res, images, name) + "." + extension;
    folderName = folderName + File.separator + res + File.separator + images;
    log.info("downloadAddr : " + downloadAddr);
    try (OutputStream outputStream = response.getOutputStream()) {
      yos.download(downloadAddr, outputStream, folderName, name, extension);
      outputStream.flush();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  private String append(String... names) {
    StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.append(names[0]);
    for (int i = 1; i < names.length; i++) {
      stringBuilder.append("/").append(names[i]);
    }
    return stringBuilder.toString();
  }
}
